package com.my.swas.sso.module.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.my.swas.common.base.ResponseResult;
import com.my.swas.common.constans.CommConst;
import com.my.swas.common.utils.BeanCopierUtil;
import com.my.swas.sso.common.base.BaseController;
import com.my.swas.sso.module.admin.dto.DictDto;
import com.my.swas.sso.module.admin.entity.Dict;
import com.my.swas.sso.module.admin.service.DictService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @author: :MaYong
 */
@Slf4j
@Controller
@RequestMapping("admin/dict")
public class DictController extends BaseController{
    @Resource
    DictService dictService;

    /**
     * 数据字典列表
     *
     * @param dictDto
     * @return
     */
    @RequiresPermissions("operate:dict:view")
    @ResponseBody
    @RequestMapping("list")
    public IPage<Dict> list(DictDto dictDto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status", CommConst.ONE);
        IPage page =  dictService.page(dictDto.getPage(), queryWrapper);
        dictService.initDict();
        return page;
    }

    /**
     * 数据字典保存
     *
     * @param dictDto
     * @return
     */
    @RequiresPermissions("operate:dict:save")
    @ResponseBody
    @RequestMapping("save")
    public ResponseResult save(DictDto dictDto) {
        Dict dict = new Dict();
        BeanCopierUtil.convert(dictDto, dict);
        dictService.saveOrUpdate(dict);
        return ResponseResult.ok;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("remove")
    @RequiresPermissions("operate:dict:delete")
    public ResponseResult batchDel(String ids) {
        if (StrUtil.isBlank(ids)) {
            return ResponseResult.error("ids没有传任何值");
        }
        List<Long> idList = Splitter.on(CommConst.COMMA).splitToList(ids)
                .stream().map(s -> Long.valueOf(s)).collect(Collectors.toList());
        List<Dict> dictList = Lists.newArrayList();
        Dict dict = null;
        for (Long id : idList) {
            dict = new Dict();
            dict.setId(id);
            dict.setStatus(CommConst.TWO);
            dictList.add(dict);
        }
        dictService.saveOrUpdateBatch(dictList);
        return ResponseResult.ok;
    }

    /**
     * 跳转到添加页面
     *
     * @param id
     * @return
     */
    @RequestMapping("toAddPage")
    public String toAddPage(Long id, Model model) {
        if (null != id) {
            Dict dict = dictService.getById(id);
            model.addAttribute("dict", dict);
            model.addAttribute("type", CommConst.TWO);
        } else {
            model.addAttribute("type", CommConst.ONE);
        }
        return "layui/system/dict/add";
    }

    /**
     * 跳转到编辑页面
     *
     * @param id
     * @return
     */
    @RequestMapping("toEditPage")
    public String toEditPage(Long id, Model model) {
        Dict dict = dictService.getById(id);
        model.addAttribute("dict", dict);
        return "layui/system/dict/edit";
    }

}
